<?php

/**
 * Created by PhpStorm.
 * User: A510L
 * Date: 2018/1/23
 * Time: 8:37
 */
class Tree extends CI_Controller{

    public function index(){$this->load->view('role_menu/viewTree');}

    public function addTree(){$this->load->view('role_menu/tree');}

    public $token_data;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('sys_menu_model');
        $this->token_data = check_token($this);
    }

    /**
     * 查询权限接口
     */
    public function imp_index(){
        if ($this->token_data['tag']) {
            $data = $this->sys_menu_model->get_All();
            $this->token_data['data'] = $data;
        }
        $json = json_encode($this->token_data['data']);
        echo $json;
    }

    /**
     * 添加权限接口
     */
    public function imp_add(){
        if ($this->token_data['tag']) {
            $data = $_POST['data'];
            $role_id = $_POST['role_id'];
            $arr = json_decode($data, true);
            $index = count($arr);
            $this->sys_menu_model->add_role_menu_del($role_id);
            for ($i = 0; $i < $index;) {
                $this->sys_menu_model->add_role_menu($role_id, $arr[$i]);
                $i++;
            }
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }

    /**
     * 回显接口
     */
    public function imp_edit(){
        if ($this->token_data['tag']) {
            $role_id = $_POST['role_id'];
            $data = $this->sys_menu_model->getMenuById($role_id);
            $this->token_data['data'] = $data;
        }
        $json = json_encode($this->token_data['data']);
        echo $json;
    }
}